package swingxrcp.framework.client.swingx;

import java.awt.Component;

import javax.swing.JPanel;

import swingxrcp.framework.client.application.Application;
import swingxrcp.framework.client.context.SwingXContext;

public class JXPage extends JPanel implements SwingX {
	private static final long serialVersionUID = -3329214109047946685L;

	private boolean running = false;

	public JXFrame getMainFrame() {
//		return (JXFrame) SwingUtilities.getWindowAncestor(this);
		return Application.mainFrame;
	}
	// TODO SwingX生命周期 -=================================================================
	public void open() {
		running = true;
	}

	public void close() {
		running = false;
		removeAttributes();
	}

	public boolean isRunning() {
		return running;
	}
	// TODO 处理Action事件 -=================================================================

	public void registerAction(Component component, String method) {
		if (component != null) {
		} else {
//			Logger.printErrorMessage("为组件添加Action事件驱动器,组件还没有初始化!  方法名:" + method);
		}
	}

	// TODO 处理Thread事件 -=================================================================
	/**
	 * 激活当前UI容器
	 */
	public void pageActivated() {

	}

	/**
	 * 执行先/后回调
	 */
	public void executeCallback() {

	}

	/**
	 * 前执行
	 */
	public void executeBefore() {
		/* *** 子类实现 *** */
	}

	/**
	 * 后执行
	 *
	 */
	public void executeAfter() {
		/* *** 子类实现 *** */
	}

	public void run() {
		// nothing
	}

	// TODO 处理缓存 -=================================================================
	public Object getAttribute(String key) {
		return SwingXContext.get(this.getClass().getName(), key);
	}

	public void setAttribute(String key, Object object) {
		SwingXContext.add(this.getClass().getName(), key, object, false);
	}

	public void setAttribute(String key, Object object, boolean lock) {
		SwingXContext.add(this.getClass().getName(), key, object, lock);
	}

	public void removeAttribute(String key) {
		SwingXContext.remove(this.getClass().getName(), key);
	}
	
	public void removeAttributes(){
		SwingXContext.removeCacheById(this.getClass().getName());
	}
}
